import React from 'react';
import {Table, Tag} from 'antd';
import dt from 'moment';

// 默认表格带边线并隔行变色
export default props => (
    <Table
        bordered
        rowClassName={(rec, idx) => ((idx % 2) ? 'odd-row' : '')}
        {...props}
    />
);

function setColKey(col) {
    if (!col.key && col.dataIndex) {
        col.key = col.dataIndex;
    }
}

export function setColumnKeys(columns) {
    columns.forEach(col => {
        setColKey(col);
        if (col.children) {
            setColumnKeys(col.children);
        }
    });
}

export const numRender = value => (
    <div className="right">{value || 0}</div>
);

export const sexRender = value => {
    switch (value) {
    case 'M':
        return '男';
    case 'F':
        return '女';
    }
};

export const yesRender = value => (
    value ? <Tag color="#87d068">是</Tag> : <Tag color="#f50">否</Tag>
);


export const dateRender = value => (value ? dt(value).format('YYYY-MM-DD') : '');
